package pure.engine.core 
{
	import pure.engine.renderer.AnimationRenderer;
	import pure.engine.renderer.IAnimation;
	import pure.engine.renderer.IRenderer;
	import pure.engine.renderer.IShape;
	import pure.engine.renderer.IText;
	import pure.engine.renderer.Renderer;
	import pure.engine.renderer.ShapeRenderer;
	import pure.engine.renderer.TextRenderer;
	import pure.engine.system.ns_despair;
	
	use namespace ns_despair;
	
	/**
	 * 可渲染对象
	 * [Author] - PURE
	 */
public class RenderableCore extends VisibleCore implements IRenderable
{
	
	
	

	//======================
	// Renderer !!
	//======================
	
		
	// 首渲染器(快速获取)
	public function get headRenderer():IRenderer { return m_rendererList[0]; }
	
	// 尾渲染器(快速获取)
	public function get tailRenderer():IRenderer { return m_rendererList[m_numRenderer - 1]; }


	/**
	 * 生成动画渲染器
	 * 
	 * [param] - [section]	片段名字
	 * [param] - [name]		渲染器名字
	 */
	public function createAnimationRenderer(sectionName:String = null, name:String = null):IAnimation
	{
		var R:AnimationRenderer;
		
		R = new AnimationRenderer();
		R.section = sectionName;
		_addRenderer(R, name)
		return R;
	}
	
	
	/**
	 * 生成图形渲染器
	 * 
	 * @param offsetX
	 * @param offsetY
	 * @param name
	 */
	public function createShapeRenderer(offsetX:Number = 0, offsetY:Number = 0, name:String = null):IShape
	{
		var R:ShapeRenderer;
		
		R = new ShapeRenderer(offsetX, offsetY);
		_addRenderer(R, name)
		return R;
	}
	
	
	/**
	 * 生成文本渲染器
	 * 
	 * @param width
	 * @param height
	 * @param text
	 * @param offsetX
	 * @param offsetY
	 * @param name
	 */
	public function createTextRenderer(text:String, offsetX:Number = 0, offsetY:Number = 0, gapX:Number = 0, gapY:Number = 0, name:String = null):IText
	{
		var R:TextRenderer;
		
		R = new TextRenderer(text, offsetX, offsetY, gapX, gapY) 
		_addRenderer(R, name)
		return R;
	}
	
	
	/**
	 * 获取渲染器(名字)
	 * 
	 * @param name
	 */
	public function getRenderer(name:String):IRenderer 
	{
		var i:int;
		
		for (i = 0; i < m_numRenderer; i++) 
		{
			if (m_rendererList[i].m_name == name) 
			{
				return m_rendererList[i];
			}
		}
		return null;
	}
	
	
	
	

	/* ----------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★★★★★★  private  ★★★★★★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------
	 */

	

	
	// 加入渲染器
	ns_despair function _addRenderer(R:Renderer, name:String):void
	{
		R.m_name                           =  name;
		m_rendererList[m_numRenderer++]    =  R;
	}
	

	// 更新渲染器
	ns_despair function updateRenderer():void
	{
		// Override !!
	}
	
	
	 // 毁灭
	override ns_despair function destroy():void
	{
		super.destroy();
		
		while (--m_numRenderer > -1)
		{
			m_rendererList[m_numRenderer].destroy();
		}
		m_rendererList.length  =  m_numRenderer  =  0;
	}
	
	
	
	
	
	
	//======================
	// Member
	//======================
	
	
	ns_despair var m_rendererList:Vector.<Renderer> = new Vector.<Renderer>();  // 渲染器列表
		
	ns_despair var m_numRenderer:int;  // 渲染器数目
	
}

}